package com.zwxbc.funplay.dao.impl;

import com.zwxbc.funplay.dao.GameDao;
import com.zwxbc.funplay.entity.Game;
import com.zwxbc.funplay.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GameDaoImpl implements GameDao {
	private static final String INSERT_GAME = "INSERT INTO game (price, developer_id, category_id, sales_count, average_rating, game_image, game_introduction, release_date, game_name) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
	private static final String SELECT_GAME_BY_ID = "SELECT * FROM game WHERE game_id = ?";
	private static final String SELECT_ALL_GAMES = "SELECT * FROM game";
	private static final String UPDATE_GAME = "UPDATE game SET price = ?, developer_id = ?, category_id = ?, sales_count = ?, average_rating = ?, game_image = ?, game_introduction = ?, release_date = ?, game_name = ? WHERE game_id = ?";
	private static final String DELETE_GAME = "DELETE FROM game WHERE game_id = ?";
	private static final String SELECT_GAME_BY_CATEGORY_ID = "SELECT * FROM game WHERE category_id = ?";
	private static final String SELECT_GAME_BY_NAME_LIKE = "SELECT * FROM game WHERE game_name LIKE ?";
	private static final String SELECT_GAME_RATING_TOP = "SELECT * FROM game ORDER BY average_rating DESC";
	@Override
	public int addGame(Game game) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(INSERT_GAME)) {
			preparedStatement.setDouble(1, game.getPrice());
			preparedStatement.setLong(2, game.getDeveloperId());
			preparedStatement.setLong(3, game.getCategoryId());
			preparedStatement.setLong(4, game.getSalesCount());
			preparedStatement.setDouble(5, game.getAverageRating());
			preparedStatement.setString(6, game.getGameImage());
			preparedStatement.setString(7, game.getGameIntroduction());
			preparedStatement.setTimestamp(8, game.getReleaseDate() != null ? new java.sql.Timestamp(game.getReleaseDate().getTime()) : null);
			preparedStatement.setString(9, game.getGameName());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public List<Game> getGameListByNameLike(String gameName) {
		List<Game> games = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_GAME_BY_NAME_LIKE)) {
			// 设置查询参数
			preparedStatement.setString(1, "%" + gameName + "%");

			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				while (resultSet.next()) {
					games.add(createGameFromResultSet(resultSet));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return games;
	}

	@Override
	public List<Game> getGameRatingTop() {
		List<Game> games = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_GAME_RATING_TOP);
		     ResultSet resultSet = preparedStatement.executeQuery()) {
			while (resultSet.next()) {
				games.add(createGameFromResultSet(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return games;
	}

	@Override
	public Game getGameById(Long gameId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_GAME_BY_ID)) {
			preparedStatement.setLong(1, gameId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createGameFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public List<Game> getGameListByCategoryId(Long gameCategoryId) {
		List<Game> games = new ArrayList<>();

		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_GAME_BY_CATEGORY_ID)) {
			// 设置查询参数
			preparedStatement.setLong(1, gameCategoryId);

			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				while (resultSet.next()) {
					games.add(createGameFromResultSet(resultSet));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}

		return games;
	}

	@Override
	public List<Game> getAllGames() {
		List<Game> games = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_GAMES);
		     ResultSet resultSet = preparedStatement.executeQuery()) {
			while (resultSet.next()) {
				games.add(createGameFromResultSet(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return games;
	}

	@Override
	public int updateGame(Game game) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_GAME)) {
			preparedStatement.setDouble(1, game.getPrice());
			preparedStatement.setLong(2, game.getDeveloperId());
			preparedStatement.setLong(3, game.getCategoryId());
			preparedStatement.setLong(4, game.getSalesCount());
			preparedStatement.setDouble(5, game.getAverageRating());
			preparedStatement.setString(6, game.getGameImage());
			preparedStatement.setString(7, game.getGameIntroduction());
			preparedStatement.setTimestamp(8, game.getReleaseDate() != null ? new java.sql.Timestamp(game.getReleaseDate().getTime()) : null);
			preparedStatement.setString(9, game.getGameName());
			preparedStatement.setLong(10, game.getGameId());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int deleteGame(Long gameId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DELETE_GAME)) {
			preparedStatement.setLong(1, gameId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	// Helper 方法，从 ResultSet 中创建 Game 对象
	private Game createGameFromResultSet(ResultSet resultSet) throws SQLException {
		Game game = new Game();
		game.setGameId(resultSet.getLong("game_id"));
		game.setGameName(resultSet.getString("game_name"));
		game.setPrice(resultSet.getDouble("price"));
		game.setDeveloperId(resultSet.getLong("developer_id"));
		game.setCategoryId(resultSet.getLong("category_id"));
		game.setSalesCount(resultSet.getLong("sales_count"));
		game.setAverageRating(resultSet.getDouble("average_rating"));
		game.setGameImage(resultSet.getString("game_image"));
		game.setGameIntroduction(resultSet.getString("game_introduction"));
		game.setReleaseDate(resultSet.getTimestamp("release_date"));
		return game;
	}
}
